<!DOCTYPE html>
<html>
<!--头文件-->
{include file='head'}

<section class="rt_wrap content mCustomScrollbar">
 <div class="rt_content">
  <div class="page_title">
   <h2 class="fl">商品详情信息</h2>
   <a class="fr top_rt_btn">返回产品列表</a>
  </div>
  <section>
   <ul class="ulColumn2">

    {foreach $goods as $g}
    <li>
     <!--上传图片-->
     <form action="{$webSite}admin/goods/upload" method="post" enctype="multipart/form-data">
      <span class="item_name" style="width:120px;">上传图片：</span>

      <input type="hidden" name="gid" value="{$g['goods_id']}">
      <label class="uploadImg">
       <img src="{$webSite}{$g['mid_logo']}" width="100">
       <input type="file" name="img"/>
      </label>
      <span class="item_name" style="width:40px;"></span>
      <input type="submit" value="上传" class="link_btn"/>
     </form>
    </li>

    <li>
     <span class="item_name" style="width:120px;">商品名称：</span>
     <input type="text" class="textbox textbox_295" value="{$g['goods_name']}"  placeholder="商品名称..." name="shopName" maxlength="30" goodsName="{$g['goods_name']}"/>
     <span class="tips"></span>
    </li>
    <li>
     <span class="item_name" style="width:120px;">商品货号：</span>
     <input type="text" class="textbox" placeholder="商品货号..." value="{$g['goods_card']}"  name="shopCard" maxlength="8" goodsCard="{$g['goods_card']}" readonly/>
     <span class="tips"></span>
    </li>
    <li>
     <span class="item_name" style="width:120px;">品牌：</span>
     <select class="select" id="sel1" goodsBrand="{$g['brand_id']}">
      <option value="0">选择产品品牌</option>
      {foreach $brand as $b}
      <option value="{$b['brand_id']}">{$b['brand_name']}</option>
      {/foreach}
     </select>
     <span class="tips"></span>
    </li>
    <li>
     <span class="item_name" style="width:120px;">分类：</span>
     <select class="select" id="sel2" goodsType="{$g['type_id']}">
      <option value="0">选择产品分类</option>
      {foreach $type as $t}
      <option value="{$t['type_id']}">{$t['type_name']}</option>
      {/foreach}
     </select>
     <span class="tips"></span>
    </li>
    <li>
     <span class="item_name" style="width:120px;">属性：</span>
     <select class="select" id="sel3" goodsAttr="{$g['attr_id']}">
      <option value="0">选择产品属性</option>
      {foreach $attr as $a}
      <option value="{$a['attr_id']}">{$a['attr_name']}</option>
      {/foreach}
     </select>
     <span class="tips"></span>
    </li>
    <li>
     <span class="item_name" style="width:120px;">重量：</span>
     <input type="text" class="textbox" placeholder="重量..." value="{$g['weight']}"   name="weight" maxlength="8" goodsWeight="{$g['weight']}"/>
     <select class="select" id="sel4" goodsUnit="{$g['weight_unit']}">
      <option value="0">重量单位</option>
      <option value="1">克</option>
      <option value="2">千克</option>
     </select>
     <span class="tips"></span>&nbsp;
     <span class="tips"></span>
    </li>
    <li>
     <span class="item_name" style="width:120px;">本店售价：</span>
     <input type="text" class="textbox" placeholder="本店售价..." value="{$g['shop_price']}"   name="shopPrice" maxlength="8" goodsPrice="{$g['shop_price']}"/>
     <span>元</span>&nbsp;&nbsp;&nbsp;
     <span class="tips"></span>
    </li>
    <li>
     <span class="item_name" style="width:120px;">市场售价：</span>
     <input type="text" class="textbox" placeholder="市场售价..." value="{$g['market_price']}"  name="marketPrice" maxlength="8" goodsMarket="{$g['market_price']}"/>
     <span>元</span>&nbsp;&nbsp;&nbsp;
     <span class="tips"></span>
    </li>
    <li>
     <span class="item_name" style="width:120px;">库存量：</span>
     <input type="text" class="textbox" placeholder="库存数量..." value="{$g['stock_num']}"  name="stockNum" maxlength="8" goodsStock="{$g['stock_num']}"/>
     <span>件</span>&nbsp;&nbsp;&nbsp;
     <span class="tips"></span>
    </li>

    <!--文本框-->
    <li>
     <span class="item_name" style="width:120px;">产品详情：</span>
     <textarea class="textbox" name="text" rows="10" cols="80" style="height:200px; resize: none;" goodsDesc="{$g['goods_desc']}" maxlength="200">{$g['goods_desc']}</textarea>
     <span class="tips"></span>
    </li>
    <li>
     <span class="item_name" style="width:120px;"></span>
     <input type="submit" class="link_btn" name="btn" goodsId="{$g['goods_id']}" site="{$webSite}">
    </li>
    {/foreach}

   </ul>
  </section>
 </div>
</section>
</body>
</html>
<script>
    //添加数据
    $(function () {

        //变量
        var $oName = null;  //商品名
        var $oCard = null;  //商品编号
        var $oBrand = null; //品牌id
        var $oType = null;  //分类id
        var $oAttr = null;  //属性id
        var $oWeight = null;//重量
        var $oUnit = null;  //重量单位
        var $oPrice = null; //本店价
        var $oMarket = null;//市场价
        var $oStock = null; //库存
        var $oDesc = '';  //描述
        var $params = []; //存放修改过的值

        //标记位
        var fName = true;
        var fCard = true;
        var fType = true;
        var fAttr = true;
        var fBrand = true;
        var fWeight = true;
        var fUnit = true;
        var fPrice = true;
        var fMarket = true;
        var fStock = true;
        var fDesc = true;


        //原数据
        var $oldName  = $('[name=shopName]').attr('goodsName');
        var $oldCard  = $('[name=shopCard]').attr('goodsCard');
        var $oldBrand = $('#sel1').attr('goodsBrand');
        var $oldType  = $('#sel2').attr('goodsType');
        var $oldAttr  = $('#sel3').attr('goodsAttr');
        var $oldUnit  = $('#sel4').attr('goodsUnit');
        var $oldWeight = $('[name=weight]').attr('goodsWeight');
        var $oldPrice   = $('[name=shopPrice]').attr('goodsPrice');
        var $oldMarket = $('[name=marketPrice]').attr('goodsMarket');
        var $oldStock  = $('[name=stockNum]').attr('goodsStock');
        var $oldDesc   = $('[name=text]').attr('goodsDesc');


        //商品名
        $('[name=shopName]').blur(function () {

            //清空
            delete $params['goods_name'];

            //取值, 动用正则,排除输入空字符符号
            $oName = $('[name=shopName]').val().replace(/(^\s*)|(\s*$)/g, "");

            //是否修改
            if ($oldName == $oName) {

                $('.tips').eq(0).addClass('errorTips').html('商品名未修改').css('color', 'blue');
                fName = true;

            } else {

                //商品名是否为空
                if ($oName.length == 0) {
                    $('.tips').eq(0).addClass('errorTips').html('商品名不得为空').css('color', 'red');
                    fName = false;
                    return false;
                }

                //利用正则判断汉字
                if ($oName.match(/^[\u4E00-\u9FA5]{1,}$/)) {
                    //alert('这是汉字');
                    if ($oName.length > 10 || $oName.length < 1) {
                        $('.tips').eq(0).addClass('errorTips').html('商品名应为 1~10 个汉字').css('color', 'red');
                        fName = false;
                        return false;
                    }

                } else {
                    //alert('这不是汉字');
                    if ($oName.length > 30 || $oName.length < 3) {
                        $('.tips').eq(0).addClass('errorTips').html('商品名应为 3~30 位字符串').css('color', 'red');
                        fName = false;
                        return false;
                    }
                }

                //标记,存储
                fName = true;
                $params['goods_name'] = $oName;

                //console.log(fName);
                $('.tips').eq(0).addClass('errorTips').html('合法').css('color', 'green');
            }
            console.log($params);
        });

        //商品编号
       /* $('[name=shopCard]').blur(function () {

           //清空
            delete $params['goods_card'];


            //获取用户名的值, 动用正则,排除输入空字符符号
            $oCard = $('[name=shopCard]').val().replace(/(^\s*)|(\s*$)/g, "");

            //是否修改
            if ($oldCard == $oCard) {

                $('.tips').eq(1).addClass('errorTips').html('商品编号未修改').css('color', 'blue');
                fCard = true;
            } else {

                //判断用户名是否为空
                if ($oCard.length == 0) {
                    $('.tips').eq(1).addClass('errorTips').html('商品编号不得为空').css('color', 'red');
                    fCard = false;
                    return false;
                }

                //console.log($oCard.match( /^[0-9a-zA-Z]*$/g));  //正则匹配返回数组
                //编号只能为数字,字母,或二者组合
                if ($oCard.length != 8 || !$oCard.match( /^[0-9a-zA-Z]*$/g)) {

                    $('.tips').eq(1).addClass('errorTips').html('编号只能为8  位数字,字母或二者组合').css('color', 'red');
                    fCard = false;
                    return false;
                }

                //标记存储
                fCard = true;
                $params['goods_card'] = $oCard;

                if (fCard) {
                    console.log($oCard);
                    //ajax传输
                    $.post('card', {card: $oCard}, success, 'json');
                }
            }


        });*/

        //品牌
        $('#sel1').change(function () {

            //清空
            delete $params['brand_id'];

            //取值
            $oBrand = $('#sel1').val();
            //console.log($oBrand);

            //是否修改
            if ($oldBrand == $oBrand) {

                $('.tips').eq(2).addClass('errorTips').html('品牌未修改').css('color', 'blue');
                fBrand = true;
            } else {

                //是否添加选项
                if ($oBrand > 0) {

                    $('.tips').eq(2).addClass('errorTips').html('品牌合法').css('color', 'green');
                    //标记,存储数据
                    fBrand = true;
                    $params['brand_id'] = $oBrand;

                } else {
                    fBrand = false;
                }
            }

        });

        //分类
        $('#sel2').change(function () {

            //清空
            delete $params['type_id'];

            $oType = $('#sel2').val();
            //console.log($oType);

            //是否修改
            if ($oldType == $oType) {

                $('.tips').eq(3).addClass('errorTips').html('分类名未修改').css('color', 'blue');
                fType = true;
            } else {

                //是否添加选项
                if ($oType > 0) {

                    $('.tips').eq(3).addClass('errorTips').html('分类名合法').css('color', 'green');
                    //标记,存储
                    fType = true;
                    $params['type_id'] = $oType;

                } else {
                    fType = false;
                }
            }

        });

        //属性
        $('#sel3').change(function () {

            //清空
            delete $params['attr_id'];

            $oAttr = $('#sel3').val();
            //console.log($oAttr);

            //是否修改
            if ($oldAttr == $oAttr) {

                $('.tips').eq(4).addClass('errorTips').html('属性名未修改').css('color', 'blue');
                fAttr = true;
            } else {

                if ($oAttr > 0) {

                    $('.tips').eq(4).addClass('errorTips').html('属性名合法').css('color', 'green');
                    //标记,存储
                    fAttr = true;
                    $params['attr_id'] = $oAttr;
                } else {
                    fAttr = false;
                }
            }

        });

        //重量
        $('[name=weight]').blur(function () {

            //清空
            delete $params['weight'];

            $oWeight = $(this).val().replace(/(^\s*)|(\s*$)/g, "");

            //是否修改
            if ($oldWeight == $oWeight) {

                $('.tips').eq(5).addClass('errorTips').html('重量未修改').css('color', 'blue');
                fWeight = true;
            } else {

                //是否为空,长度0~8位, 只能为数字,包括小数,大于0
                if ($oWeight.length == 0 || $oWeight.length > 8 || isNaN($oWeight) || $oWeight <= 0) {
                    $('.tips').eq(5).addClass('errorTips').html('重量只能为1~8位,大于0 的纯数字').css('color', 'red');
                    fWeight = false;
                    return false;
                } else {
                    //标记,存储
                    fWeight = true;
                    $params['weight'] = $oWeight;

                    $('.tips').eq(5).addClass('errorTips').html('重量合法').css('color', 'green');
                }
            }
        });

        //重量单位
        $('#sel4').change(function () {

            //清空
            delete $params['weight_unit'];

            $oUnit = '';
            $oUnit = $('#sel4').val();
            //console.log('unit:'+ $oUnit);
            //console.log('oldUnit:'+ $oldUnit);

            //处理oUnit
            var $oUnit2 = null;
            if ($oldUnit == 'g') {

                $oUnit2 = 1;
            } else if ($oldUnit == 'kg'){

                $oUnit2 = 2;
            }
            //console.log('unit2:'+ $oUnit2);

            //是否修改
            if ($oUnit2 == $oUnit) {

                $('.tips').eq(6).html('单位名未修改').css('color', 'blue');
                fUnit = true;

            } else {

                if ($oUnit > 0) {

                    $('.tips').eq(6).html('单位名合法').css('color', 'green');

                    //标记,存储
                    fUnit = true;
                    $params['weight_unit'] = $oUnit;

                } else {

                    fUnit = false;
                }

            }
        });

        //本店售价
        $('[name=shopPrice]').blur(function () {

            //清空
            delete $params['shop_price'];

            $oPrice = $(this).val().replace(/(^\s*)|(\s*$)/g, "");

            //是否修改
            if ($oldPrice == $oPrice) {

                $('.tips').eq(7).addClass('errorTips').html('本店售价未修改').css('color', 'blue');
                fPrice = true;
            } else {

                //是否为空,长度0~8位, 只能为数字,包括小数,大于0
                if ($oPrice.length == 0 || $oPrice.length > 8 || isNaN($oPrice) || $oPrice <= 0) {
                    $('.tips').eq(7).addClass('errorTips').html('本店售价只能为1~8位,大于0 的纯数字').css('color', 'red');
                    fPrice = false;
                    return false;
                }

                //标记存储
                fPrice = true;
                $params['shop_price'] = $oPrice;

                $('.tips').eq(7).addClass('errorTips').html('本店售价合法').css('color', 'green');
            }
        });

        //市场价
        $('[name=marketPrice]').blur(function () {

            //清空
            delete $params['market_price'];

            $oMarket = $(this).val().replace(/(^\s*)|(\s*$)/g, "");
            //console.log($oMarket);

            //是否修改
            if ($oldMarket == $oMarket) {

                $('.tips').eq(8).addClass('errorTips').html('市场价未修改').css('color', 'blue');
                fMarket = true;

            } else {

                //是否为空,长度0~8位, 只能为数字,包括小数,大于0
                if ($oMarket.length == 0 || $oMarket.length > 8 || isNaN($oMarket) || $oMarket <= 0) {
                    $('.tips').eq(8).addClass('errorTips').html('市场价只能为1~8位,大于0 的纯数字').css('color', 'red');
                    fMarket = false;
                    return false;
                }

                //标记,存储
                fMarket = true;
                $params['market_price'] = $oMarket;

                $('.tips').eq(8).addClass('errorTips').html('市场价合法').css('color', 'green');
            }
        });

        //库存量
        $('[name=stockNum]').blur(function () {

            //清空
            delete $params['stock_num'];

            $oStock = $(this).val().replace(/(^\s*)|(\s*$)/g, "");
            //console.log($oStock);

            //是否修改
            if ($oldStock == $oStock) {

                $('.tips').eq(9).addClass('errorTips').html('库存量未修改').css('color', 'blue');
                fStock = true;
            } else {

                //是否为空,长度0~8位, 只能为数字,包括小数,大于0
                if ($oStock.length == 0 || $oStock.length > 8 || isNaN($oStock) || $oStock < 0) {
                    $('.tips').eq(9).addClass('errorTips').html('库存量只能为1~8位,大于0 的纯数字').css('color', 'red');
                    fStock = false;
                    return false;
                }

                //标记,存储
                fStock = true;
                $params['stock_num'] = $oStock;

                $('.tips').eq(9).addClass('errorTips').html('库存量合法').css('color', 'green');
            }
        });

        //商品描述
        $('[name=text]').blur(function () {

            //清空
            delete $params['goods_desc'];

            $oDesc = $(this).val().replace(/(^\s*)|(\s*$)/g, "");
            //console.log($oDesc);
            //console.log($oDesc.length);

            //是否修改
            if ($oldDesc == $oDesc) {

                $('.tips').eq(10).addClass('errorTips').html('商品描述未修改').css('color', 'blue');
                fDesc = true;

            } else {

                //是否为汉字
                if ($oDesc.match(/^[\u4E00-\u9FA5]{1,}$/)) {
                    alert(6666666666666666);
                    //80个汉字之内
                    if ($oDesc.length > 80) {

                        $('.tips').eq(10).addClass('errorTips').html('不得超过80个汉字').css('color', 'red');
                        fDesc = false;
                        return false;
                        alert(343545323);
                    }
                } else {

                    //240个字符之内
                    if ($oDesc.length > 200) {

                        $('.tips').eq(10).addClass('errorTips').html('字数不得超过200个字符').css('color', 'red');
                        fDesc = false;
                        return false;
                    }
                }

                //标记,存储
                fDesc = true;
                $params['goods_desc'] = $oDesc;

                $('.tips').eq(10).addClass('errorTips').html('商品描述合法').css('color', 'green');

            }

        });

        //按钮提交
        $('[name=btn]').click(function () {

            //ajax传输
            if (fName && fCard && fWeight && fUnit && fType && fAttr && fBrand && fPrice && fMarket && fStock) {

                //console.log(Object.keys($params).length);
                //有修改数据
                if (Object.keys($params).length > 0) {

                    var $gid = $(this).attr('goodsId');
                    var $oSite = $(this).attr('site');
                    //console.log($oSite);
                    var $arr = []; //存放处理后的数组
                    //遍历修改数组,插入堆栈中,拼接成字符串
                    for (var key in $params) {

                        var $k = key;
                        var $v = $params[key];
                        if ($params[key]) {

                            $arr.push($k + '===' + $v);
                        }
                    }
                    var $str = $arr.join('++_++'); //拼接符: ++_++
                    //console.log($str);
                    //console.log($gid);

                    //ajax
                    //$.post('update',{gid:$gid,str:$str},success,'json');
                    $.post($oSite + 'admin/goods/update',{gid:$gid,str:$str},success,'json');


                } else {

                    alert('未修改当前商品信息');
                }

            } else {
                //商品名
                if (!fName) {
                    $('.tips').eq(0).addClass('errorTips').html('商品名称不合法').css('color', 'red');
                }
                //商品编号
                if (!fCard) {
                    $('.tips').eq(1).addClass('errorTips').html('商品编号不合法').css('color', 'red');
                }

                //品牌
                if (!fBrand) {
                    $('.tips').eq(2).addClass('errorTips').html('品牌名不得为空').css('color', 'red');
                }

                //分类
                if (!fType) {
                    $('.tips').eq(3).addClass('errorTips').html('分类名不得为空').css('color', 'red');
                }

                //属性
                if (!fAttr) {
                    $('.tips').eq(4).addClass('errorTips').html('属性名不得为空').css('color', 'red');
                }

                //重量
                if (!fWeight) {
                    $('.tips').eq(5).addClass('errorTips').html('重量不得为空').css('color', 'red');
                }

                //重量单位
                if (!fUnit) {
                    $('.tips').eq(6).addClass('errorTips').html('单位选项不得为空').css('color', 'red');
                }

                //本店售价
                if (!fPrice) {
                    $('.tips').eq(7).addClass('errorTips').html('本店售价不得为空').css('color', 'red');
                }

                //市场价
                if (!fMarket) {
                    $('.tips').eq(8).addClass('errorTips').html('市场价不得为空').css('color', 'red');
                }

                //库存量
                if (!fStock) {
                    $('.tips').eq(9).addClass('errorTips').html('库存量不得为空').css('color', 'red');
                }

                alert('请先按上述要求完成操作');
                return false;
            }
        });

        function success(data)
        {
            //console.log(data);
            switch (data.state) {

                //商品编号
                case 1:
                    $('.tips').eq(1).addClass('errorTips').html(data.tip).css('color', 'red');
                    fCard = false;
                    break;
                //编号合法
                case 2:
                    $('.tips').eq(1).html(data.tip).css('color', 'green');
                    break;
                //添加成功
                case 3:
                    alert(data.tip);
                    //重载当前页面
                    window.location.reload();
                    break;
                //添加失败
                case 4:
                    alert(data.tip);
                    //重载当前页面
                    window.location.reload();
                    break;
            }

        }
    });


</script>